<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>学生成绩管理系统</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="/plugins/bootstraptable/bootstrap-table.min.css">
    <script src="/plugins/bootstraptable/bootstrap-table.min.js"></script>
    <script src="/plugins/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>
</head>
<body>

<!--教师权限-->
<div class="container-fluid" >

    <div style="margin: 20px 20px">
        <font size="5px">页面切换：</font>
        <a href="/teacher/tlist"><font size="4px">教师表&nbsp&nbsp&nbsp&nbsp</font></a>
        <a href="/sc/sclist"><font size="4px">成绩表&nbsp&nbsp&nbsp&nbsp</font></a>
        <a href="/course/clist"><font size="4px">授课表&nbsp&nbsp&nbsp&nbsp</font></a>
        <a href="/login/user"><font size="4px" style="margin-left:950px">退出登录</font></a>
    </div>

    <h2 align =center>学生表</h2>
    <form class="form-inline">
        <div class="form-group">
            <input type="text" class="form-control" id="searchName" name="searchName" placeholder="请输入要查询的学生名字">
        </div>
        <button type="button" class="btn btn-default btn-primary" onclick="searchStudent();">查询</button>
        <button type="button" class="btn btn-primary btn-primary" data-toggle="modal" data-target="#myModal" style="margin-left: 25px"  onclick="preAdd();">
            <font size="3px">新增学生</font>
        </button>
    </form>

    <table class="table table-bordered table-striped table-hover" style="table-layout:fixed;" id="stuTable">
    </table>
</div>

<div class="modal fade" tabindex="-1" role="dialog" id="myModal" >
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body">
                <form id="studentForm">
                    <input hidden="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label for="sno">学号</label>
                        <input type="text" class="form-control" id="sno" name="sno" placeholder="学号">
                    </div>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="1">男</option>
                            <option value="2">女</option>
                            <option value="3">未知</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" name="age" placeholder="年龄">
                    </div>
                </form>
                <!--表单结束-->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveStudent();">保存</button>
            </div>
        </div>
    </div>
</div>

<script src="/js/bootstrap.min.js"></script>
<script src="/plugins/bootstraptable/bootstrap-table.min.js"></script>
<script src="/plugins/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>

<script type="text/javascript">

    //查询
    function searchStudent(){
        $('#stuTable').bootstrapTable("destroy");
        loadTable();
    }
    // function searchStudent(){
    //     var name=$("#searchName").val();
    //     console.log("name:"+name)
    //     getdata(name);
    //     return false;
    // }
    // function getdata(name){
    //     var url="/webapi/student/findByNameLike";
    //     if(name!=null && name!=""){
    //         url +="?name="+name;
    //     }
    //     $.ajax({
    //         url:url
    //     }).done(function (rs) {
    //         var len = rs.length;
    //         var htmlStr = "";
    //         for (var i = 0; i < len; i++) {
    //             var item = rs[i];
    //             htmlStr += "<tr>"
    //                 + "<td>" + item.id + "</td>"
    //                 + "<td>" + item.sno + "</td>"
    //                 + "<td>" + item.name + "</td>"
    //                 + "<td>" + item.age+ "</td>"
    //                 + "<td>" + item.sex + "</td>"
    //                 +"<td> <a href='#' onclick='editStudent("+item.id+");'>编辑</a>  <a href='#' onclick='deleteStudent("+item.id+");'>删除</a></td>"
    //                 + "</tr>";
    //         }
    //         $("#studentTb").html(htmlStr);
    //     })
    // }
    // $(function(){
    //     getdata();
    // });

    function  preAdd(){
        $("#id").val(0);//提示新增
        $("#id").val('');
        $("#sno").val('');
        $("#name").val('');
        $("#age").val('');
        $("#password").val('');
        $("#sex").val('');
    }

    //新增或更新
    function saveStudent(){
        var data=$("#studentForm").serialize();
        var id=$("#id").val();
        if(id<1){
            //新增
            $.ajax({
                url:"/webapi/student/insert",
                method:"post",
                data:data
            }).done(function (){
                $('#stuTable').bootstrapTable("refresh");
                $('#myModal').modal('hide')
            })
        }
        else{
            //更新
            $.ajax({
                url:"/webapi/student/update",
                method:"put",
                data:data
            }).done(function (){
                $('#stuTable').bootstrapTable("refresh");
                $('#myModal').modal('hide')
            })
        }
    }

    //编辑
    function editStudent(id){
        $.ajax({
            url:'/webapi/student/get/'+id
        }).done(function (rs){
            $("#id").val(rs.id);
            $("#sno").val(rs.sno);
            $("#name").val(rs.name);
            $("#password").val(rs.password);
            $("#sex").val(rs.sex);
            $("#age").val(rs.age);
            $('#myModal').modal('show');
        })
        loadTable();
    }


    //删除
    function deleteStudent(id){
        if(confirm("确认要删除该学生信息?")){
            $.ajax({
                url:'/webapi/student/delete/'+id,
                method:"delete"
            }).done(function(){
                $('#stuTable').bootstrapTable("refresh");//表格自动更新
            });
        }
    }

    function loadTable(){
        $('#stuTable').bootstrapTable({
            url:'/webapi/student/getByPage',
            striped:true,//true隔行变色效果
            pagination:true,//true在底部显示分页条
            singleSelect: false,//true禁止多选
            pageSize:5,//如果设置了分页，每条数据条数
            pageNumber:1,//如果设置了分页,首页页码
            sidePagination: "server",//设置在哪里进行分页，可选值为“client”
            queryParams:function (params){
                var paraObj={
                    size: params.limit,
                    page:params.offset/params.limit,
                    sort:"id",
                    direct:"desc",
                    name:$("#searchName").val()
                };
                return paraObj;
            },
            columns:[{
                field:'id',
                title:'序号',
                align : 'center'
            },{
                field:'sno',
                title:'学号',
                align : 'center'
            },{
                field:'name',
                title:'姓名',
                align : 'center'
            },{
                field:'password',
                title:'密码',
                align : 'center'
            },{
                field:'sex',
                title:'性别',
                align : 'center',
                formatter: function(name) {
                    if(name==1){
                        return "男";
                    }else if(name==2){
                        return "女";
                    }else{
                        return "未知";
                    }
                }
            },{
                field:'age',
                title:'年龄',
                align : 'center'
            },{
                field: 'id',
                title:'操作',
                align : 'center',
                formatter:function (value,row,index){
                    var e='<a href="#" onclick="editStudent(\''
                        + row.id + '\')">编辑 </a>';
                    var f='<a href="#" onclick="deleteStudent(\''
                        + row.id + '\')">删除</a>';
                    return e+f;
                }
            }]
        });
    }//end of loadTable()
    $(function(){
        loadTable();
        $('#myModal').modal('hide')
    })

</script>
</body>
</html>



